package com.edu.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.commonutils.R;
import com.edu.eduservice.domain.CourseQuery;
import com.edu.eduservice.domain.EduCourse;
import com.edu.eduservice.domain.view.CourseInfoVo;
import com.edu.eduservice.domain.view.CoursePublishVo;
import com.edu.eduservice.service.EduCourseService;
import com.edu.servicebase.exceptionhandler.MyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ljw
 * @since 2022-09-16
 */
@Api(description = "课程接口")
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/educourse")
public class EduCourseController {

    @Autowired(required = false)
    private EduCourseService eduCourseService;

    //添加基本课程信息
    @ApiOperation(value = "添加课程并且添加课程详细")
    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody CourseInfoVo courseInfoVo){
       R r= this.eduCourseService.addCourse(courseInfoVo);
        return r;
    }

    //点击上一步回显
    @ApiOperation(value = "根据编号查询出课程和课程详细")
    @GetMapping("/{courseid}")
    @ResponseBody
    public R byid(@PathVariable String courseid){
        R r=this.eduCourseService.byid(courseid);
        return r;
    }



    //修改基本课程信息
    @ApiOperation(value = "修改课程并且添加课程详细")
    @PostMapping("/update")
    @ResponseBody
    public R updateid(@RequestBody CourseInfoVo courseInfoVo){
        R r= this.eduCourseService.updatecourse(courseInfoVo);
        return r;
    }

    //课程发布信息
    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("/coursepublish/{id}")
    @ResponseBody
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        //获取发布的数据
        CoursePublishVo courseInfoForm = this.eduCourseService.getCoursePublishVoById(id);
        return R.ok().data("item", courseInfoForm);
    }

    @ApiOperation("修改课程发布状态")
    @GetMapping("/puhlish/{id}")
    @ResponseBody
    public R publish(@PathVariable String id){
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");
        boolean b = this.eduCourseService.updateById(course);
        R r=null;
        //判断是否成功 并且封装数据
        if(b)
            r=R.ok();
        else
            r=R.error();
        return r;
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            CourseQuery courseQuery){

        Page<EduCourse> pageParam = new Page<>(page, limit);

        eduCourseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("删除课程 和课程所有")
    @DeleteMapping("/list/{id}")
    @ResponseBody
    public R delcourse(@PathVariable String id) throws MyException {

        this.eduCourseService.delCourse(id);

        return R.ok();
    }

}

